auto()
if (!floaty.checkPermission()) {
  // 没有悬浮窗权限，提示用户并跳转请求
  toast('本脚本需要悬浮窗权限来显示悬浮窗，请在随后的界面中允许并重新运行本脚本。')
  floaty.requestPermission()
  exit()
} else {
  // toastLog('已有悬浮窗权限');
}
// if(!requestScreenCapture()){
//   toast('请求截图失败')
//   exit()
// }
const filePath = './'
// const filePath = '/sdcard/autojs/'
const _fp = function (path) {
  return filePath + path
}
// const actionControl = require(_fp('action.js'))
// const imageControl = require(_fp('imageControl.js'))

setInterval(() => {}, 1000)
var window = floaty.window(
  <frame>
    <button id="action" text="开始运行" w="40" h="40" bg="#77ffffff" />
  </frame>
)
window.exitOnClose()

var execution = null
//记录按键被按下时的触摸坐标
var x = 0,
  y = 0
//记录按键被按下时的悬浮窗位置
var windowX, windowY
//记录按键被按下的时间以便判断长按等动作
var downTime

window.action.setOnTouchListener(function (view, event) {
  switch (event.getAction()) {
    case event.ACTION_DOWN:
      x = event.getRawX()
      y = event.getRawY()
      windowX = window.getX()
      windowY = window.getY()
      downTime = new Date().getTime()
      return true
    case event.ACTION_MOVE:
      //移动手指时调整悬浮窗位置
      window.setPosition(windowX + (event.getRawX() - x), windowY + (event.getRawY() - y))
      //如果按下的时间超过1.5秒判断为长按，退出脚本
      if (new Date().getTime() - downTime > 1500) {
        // exit();
      }
      return true
    case event.ACTION_UP:
      //手指弹起时如果偏移很小则判断为点击
      if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5) {
        onClick()
      }
      return true
  }
  return true
})
var options = [
  '一键日常',
  '你从没有见过的船新版本',
  '高粱/小麦',
  '寻宝',
  '割稻草',
  '走街串巷',
  '答题',
  '关闭脚本'
]
var optionsKey = {
  '高粱/小麦': {
    action: 'xiaomai',
    params: false
  },
  寻宝: {
    action: 'gongfa',
    params: false
  },
  割稻草: {
    action: 'daocao',
    params: false
  },
  走街串巷: {
    action: 'zoujie',
    params: false
  },
  洁净厂区: {
    action: 'jiejingchangqu',
    params: false
  },
  答题: {
    action: 'wenda',
    params: false
  }
}
function onClick() {
  const text = window.action.getText()
  console.log('text', text)
  // actionControl.didida(5)
  if (window.action.getText() == '开始运行') {
    dialogs.select('请走到滴答答任务边上再开始选择', options).then(i => {
      if (i >= 0) {
        switch (options[i]) {
          case '一键日常':
            execution = engines.execScriptFile(_fp('action.js'))
            setTimeout(() => {
              execution.getEngine().emit('action', 'all', 3)
            }, 2000)

            // dialogs.singleChoice('滴答答在第几个日程', [2,3], ).then(k=>{
            //   execution.getEngine().emit("action", "all", k+2)
            // })
            break
          case '高粱/小麦':
          case '寻宝':
          case '割稻草':
          case '走街串巷':
          case '生产线':
          case '洁净厂区':
          case '答题':
            execution = engines.execScriptFile(_fp('action.js'))
            var fun = optionsKey[options[i]]
            setTimeout(() => {
              execution.getEngine().emit('action', fun.action, fun.params)
            }, 2000)
            break
          case '你从没有见过的船新版本':
            execution = engines.execScriptFile(_fp('action_auto.js'))
            break
          case '关闭脚本':
            if (execution) {
              execution.getEngine().forceStop()
            }
            window.close()
            break
          default:
            break
        }
      } else {
        toast('您取消了选择')
      }
    })

    // setTimeout(function(){
    //   execution.getEngine().emit("action", "ocr", [205,207,438,53]);
    // }, 2000);
    //
    window.action.setText('停止运行')
  } else {
    if (execution) {
      execution.getEngine().forceStop()
    }
    window.action.setText('开始运行')
  }
}
